package com.zyf.system.controller;


import com.querydsl.core.types.Predicate;
import com.zyf.system.controller.dto.DepartmentAddDto;
import com.zyf.system.controller.vo.DepartmentTreeVo;
import com.zyf.system.controller.dto.DepartmentUpdateDto;
import com.zyf.system.db.mysql.Department;
import com.zyf.system.db.mysql.QDepartment;
import com.zyf.system.repository.DepartmentRepository;
import com.zyf.system.service.DepartmentService;
import com.zyf.system.until.Constant;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @author 张云帆
 * 2025/3/26 14:32
 */
@RestController
@RequestMapping("/department")
public class DepartmentController extends BaseController{
    @Autowired
    private DepartmentRepository departmentRepository;
    private final QDepartment qDepartment = QDepartment.department;
    @Autowired
    private DepartmentService departmentService;


    @GetMapping("/getTree")
    @ApiOperation(value = "查询科室树")
    public Map<String, Object> getTree(){
        try {
            List<DepartmentTreeVo> departmentTree = departmentService.getDepartmentTree();
            return createResponse(Constant.SUCCESS, "查询成功", departmentTree);
        }catch (Exception e){
            return createResponse(Constant.FAIL, "查询科室树异常", e.getMessage());
        }
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询科室列表")
    public Map<String, Object> list(){
        try {
            Predicate predicate = qDepartment.isDeleted.eq(false);
            Iterable<Department> all = departmentRepository.findAll(predicate);
            return createResponse(Constant.SUCCESS, "查询科室列表成功", all);
        }catch (Exception e){
            return createResponse(Constant.FAIL, "查询科室列表异常", e.getMessage());
        }
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加科室")
    public Map<String, Object> add(@Validated @RequestBody DepartmentAddDto departmentAddDto){
        try {
            if(!departmentRepository.exists(qDepartment.id.eq(departmentAddDto.getParentId()))){
                return createResponse(Constant.FAIL, "父科室不存在", null);
            }
            Department department = new Department(departmentAddDto);
            departmentRepository.save(department);
            return createResponse(Constant.SUCCESS, "添加科室成功", null);
        }catch (Exception e){
            return createResponse(Constant.FAIL, "添加科室异常", e.getMessage());
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改科室")
    public Map<String, Object> update(@Validated @RequestBody DepartmentUpdateDto departmentUpdateDto){
        try {
            Department department = departmentRepository.findById(departmentUpdateDto.getId()).orElse(null);
            if(department == null){
                return createResponse(Constant.FAIL, "科室不存在", null);
            }
            if(!departmentRepository.exists(qDepartment.id.eq(departmentUpdateDto.getParentId()))){
                return createResponse(Constant.FAIL, "父科室不存在", null);
            }
            department.setName(departmentUpdateDto.getName());
            if(!departmentUpdateDto.getDescription().isEmpty()){
                department.setDescription(departmentUpdateDto.getDescription());
            }
            department.setParentId(departmentUpdateDto.getParentId());
            return createResponse(Constant.SUCCESS, "修改科室成功", departmentRepository.save(department));
        }catch (Exception e){
            return createResponse(Constant.FAIL, "修改科室异常", e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除科室")
    public Map<String, Object> delete(@Validated @NotNull @RequestParam Integer id){
        try {
            Department department = departmentRepository.findById(id).orElse(null);
            if(department == null){
                return createResponse(Constant.FAIL, "科室不存在", null);
            }
            if(departmentRepository.exists(qDepartment.parentId.eq(id))){
                return createResponse(Constant.FAIL, "科室存在子科室，不能删除", null);
            }
            departmentRepository.delete(department);
            return createResponse(Constant.SUCCESS, "删除科室成功", null);
        }catch (Exception e){
            return createResponse(Constant.FAIL, "删除科室异常", e.getMessage());
        }
    }

    @PostMapping("/changeUsable")
    @ApiOperation(value = "修改科室可用状态")
    public Map<String, Object> changeUsable(@Validated @NotNull @RequestParam Integer id){
        try {
            Department department = departmentRepository.findById(id).orElse(null);
            if(department == null){
                return createResponse(Constant.FAIL, "科室不存在", null);
            }
            if(departmentRepository.exists(qDepartment.parentId.eq(id))){
                return createResponse(Constant.FAIL, "科室存在子科室，不能修改可用状态", null);
            }
            department.setIsDeleted(!department.getIsDeleted());
            return createResponse(Constant.SUCCESS, "修改科室可用状态成功", departmentRepository.save(department));
        }catch (Exception e){
            return createResponse(Constant.FAIL, "修改科室可用状态异常", e.getMessage());
        }
    }

    @GetMapping("/get")
    @ApiOperation(value = "查询科室")
    public Map<String, Object> get(@Validated @NotNull @RequestParam Integer id){
        try {
            Department department = departmentRepository.findById(id).orElse(null);
            if(department == null){
                return createResponse(Constant.FAIL, "科室不存在", null);
            }
            if(department.getIsDeleted()){
                return createResponse(Constant.FAIL, "科室已停用", null);
            }
            return createResponse(Constant.SUCCESS, "查询科室成功", department);
        }catch (Exception e){
            return createResponse(Constant.FAIL, "查询科室异常", e.getMessage());
        }
   }

}
